From b41cca85238ea5e5937c3e4ac0c1457c1a9ae566 Mon Sep 17 00:00:00 2001 From: Tim Caswell Date: Wed, 12 Feb 2020 16:23:50 -0600 Subject: [PATCH] Add sample showing how to configure persistence for localStorage --- local-storage/.eslintrc.js | 28 ++++++++++++++++++++ local-storage/.gitignore | 5 ++++ local-storage/.npmignore | 5 ++++ local-storage/app.package | 4 +++ local-storage/manifest.xml | 17 ++++++++++++ local-storage/package.json | 26 +++++++++++++++++++ local-storage/res/.placeholder | 1 + local-storage/rollup.config.js | 16 ++++++++++++ local-storage/src/app.ts | 47 ++++++++++++++++++++++++++++++++++ local-storage/src/main.ts | 12 +++++++++ local-storage/tsconfig.json | 13 ++++++++++ 11 files changed, 174 insertions(+) create mode 100644 local-storage/.eslintrc.js create mode 100644 local-storage/.gitignore create mode 100644 local-storage/.npmignore create mode 100644 local-storage/app.package create mode 100644 local-storage/manifest.xml create mode 100644 local-storage/package.json create mode 100644 local-storage/res/.placeholder create mode 100644 local-storage/rollup.config.js create mode 100644 local-storage/src/app.ts create mode 100644 local-storage/src/main.ts create mode 100644 local-storage/tsconfig.json diff --git a/local-storage/.eslintrc.js b/local-storage/.eslintrc.js new file mode 100644 index 0000000..9ad0889 --- /dev/null +++ b/local-storage/.eslintrc.js @@ -0,0 +1,28 @@ +// ESLint config for MagicScript Apps +module.exports = { + env: { + "es6": true + }, + globals: { + // These globals are provided by the vm itself. + "print": true, + "globalThis": true, + // The following globals are provided by `magic-script-polyfills` + "setTimeout": true, + "clearTimeout": true, + "setInterval": true, + "clearInterval": true, + "setImmediate": true, + "clearImmediate": true, + "fetch": true, + "Headers": true, + "Request": true, + "Response": true, + + }, + parserOptions: { + "ecmaVersion": 2018, + "sourceType": "module" + }, + extends: "semistandard", +}; \ No newline at end of file diff --git a/local-storage/.gitignore b/local-storage/.gitignore new file mode 100644 index 0000000..1965231 --- /dev/null +++ b/local-storage/.gitignore @@ -0,0 +1,5 @@ +/.out/ +/node_modules/ +/bin/ +!/bin/index.js +/digest.sha512.signed diff --git a/local-storage/.npmignore b/local-storage/.npmignore new file mode 100644 index 0000000..1965231 --- /dev/null +++ b/local-storage/.npmignore @@ -0,0 +1,5 @@ +/.out/ +/node_modules/ +/bin/ +!/bin/index.js +/digest.sha512.signed diff --git a/local-storage/app.package b/local-storage/app.package new file mode 100644 index 0000000..cc4c1fa --- /dev/null +++ b/local-storage/app.package @@ -0,0 +1,4 @@ +DATAS = "digest.sha512.signed" : "." \ + "res/" : "res/" \ + "bin/" : "bin/" +OPTIONS = package/minApiLevel/2 diff --git a/local-storage/manifest.xml b/local-storage/manifest.xml new file mode 100644 index 0000000..9e4859a --- /dev/null +++ b/local-storage/manifest.xml @@ -0,0 +1,17 @@ + + + + + + + + + diff --git a/local-storage/package.json b/local-storage/package.json new file mode 100644 index 0000000..c2f4e9f --- /dev/null +++ b/local-storage/package.json @@ -0,0 +1,26 @@ +{ + "scripts": { + "build": "rollup -c", + "start": "magic-script run" + }, + "private": true, + "devDependencies": { + "eslint-config-semistandard": "^13.0.0", + "eslint-config-standard": "^12.0.0", + "eslint-plugin-import": "^2.16.0", + "eslint-plugin-node": "^8.0.1", + "eslint-plugin-promise": "^4.0.1", + "eslint-plugin-standard": "^4.0.0", + "magic-script-typings": "^1.6.0", + "rollup": "^1.1.2", + "rollup-plugin-commonjs": "^9.3.4", + "rollup-plugin-node-resolve": "^4.0.0", + "rollup-plugin-typescript": "^1.0.1", + "semistandard": "^13.0.1", + "tslib": "^1.10.0", + "typescript": "^3.6.4" + }, + "dependencies": { + "magic-script-polyfills": "^2.8.0" + } +} diff --git a/local-storage/res/.placeholder b/local-storage/res/.placeholder new file mode 100644 index 0000000..4548892 --- /dev/null +++ b/local-storage/res/.placeholder @@ -0,0 +1 @@ +This folder is a placeholder for app assets. \ No newline at end of file diff --git a/local-storage/rollup.config.js b/local-storage/rollup.config.js new file mode 100644 index 0000000..155908d --- /dev/null +++ b/local-storage/rollup.config.js @@ -0,0 +1,16 @@ +// Rollup config for consuming some npm modules in MagicScript + +import resolve from 'rollup-plugin-node-resolve'; +import commonjs from 'rollup-plugin-commonjs'; +import typescript from 'rollup-plugin-typescript'; + +export default { + external: ['uv', 'lumin', 'ssl'], + input: 'src/main.ts', + preserveModules: true, + output: { + dir: 'bin', + format: 'es' + }, + plugins: [resolve(), commonjs(), typescript()] +}; diff --git a/local-storage/src/app.ts b/local-storage/src/app.ts new file mode 100644 index 0000000..b8b1eb9 --- /dev/null +++ b/local-storage/src/app.ts @@ -0,0 +1,47 @@ +import { LandscapeApp, ui } from 'lumin'; +const { UiText, EclipseLabelType, Alignment, HorizontalTextAlignment } = ui; + +export class App extends LandscapeApp { + onAppStart () { + // Create a new prism that's half a meter cubed. + let prism = this.requestNewPrism([0.5, 0.5, 0.5]); + + // Configure localStorage from polyfills to persist state to disk. + // This function expects a folder it can write to, don't put anything else in here. + localStorage.updateBase(this.getWritablePathWhenUnlocked() + "localStorage"); + + // First we read the old value from localStorage. + // If this is our initial load, it will be null, otherwise, it will be a string. + const oldCount = localStorage.getItem('count'); + + // Convert the old value to a number (defaulting to zero) and increment. + const count = Number(oldCount || '0') + 1; + + // Store the new count so next time, it will increment again. + localStorage.setItem('count', String(count)); + + + // Create a nice text label using UIKit. + let text = UiText.CreateEclipseLabel( + prism, + 'Hello\nMagicScript! times ' + count, + EclipseLabelType.kT7 + ); + text.setAlignment(Alignment.CENTER_CENTER); + text.setTextAlignment(HorizontalTextAlignment.kCenter); + + // Attach the label to the root of the prism's scene graph. + prism.getRootNode().addChild(text); + } + + // Known Issue + // Web Inspector does not work unless the updateLoop function is present in source. + // It can be removed for production code. + updateLoop (delta: number) { + return true; + } + + init () { + return 0; + } +} diff --git a/local-storage/src/main.ts b/local-storage/src/main.ts new file mode 100644 index 0000000..5ba100f --- /dev/null +++ b/local-storage/src/main.ts @@ -0,0 +1,12 @@ +// Add support for things like setTimeout, setInterval and fetch. +// Simply importing this sets all these as global definitions. +// They are declared in the .eslintrc so your editor won't complain. +import 'magic-script-polyfills'; + +// Load main app logic from the app class. +import { App } from './app'; + +// Launch our app! +// The 0.5 value is the number of seconds to call `updateLoop` in an interval if +// there are no other events waking the event loop. +export default new App(0.5); diff --git a/local-storage/tsconfig.json b/local-storage/tsconfig.json new file mode 100644 index 0000000..7142324 --- /dev/null +++ b/local-storage/tsconfig.json @@ -0,0 +1,13 @@ +{ + "compilerOptions": { + "allowJs": true, + "noEmit": true, + "strict": true, + "target": "esnext", + "jsx": "react", + "moduleResolution": "node", + "allowSyntheticDefaultImports": true, + "esModuleInterop": true, + "typeRoots" : ["./node_modules/magic-script-typings", "./node_modules/@types"] + } +}