From 2f1444aac4b08d8e47074ad0c433c06f468a27ac Mon Sep 17 00:00:00 2001 From: wkj <278599135@qq.com> Date: Mon, 20 Jun 2022 15:16:39 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/debugStore/debugStore.d.ts | 2 +- lib/debugStore/debugStore.js | 2 +- lib/debugStore/index.js | 34 ++++++++++- lib/index.js | 6 -- lib/initialize.js | 6 +- lib/platforms/wechatMp/i18n/index.d.ts | 5 ++ lib/platforms/wechatMp/i18n/index.js | 2 +- lib/platforms/wechatMp/index.d.ts | 9 +-- lib/platforms/wechatMp/index.js | 39 +++++++----- src/debugStore/debugStore.ts | 2 +- src/debugStore/index.ts | 36 ++++++++++- src/index.ts | 7 +-- src/initialize.ts | 8 +-- src/platforms/wechatMp/i18n/index.ts | 6 ++ src/platforms/wechatMp/index.ts | 85 ++++++++++++++++++-------- src/typings/polyfill.d.ts | 6 -- tsconfig.json | 4 +- 17 files changed, 175 insertions(+), 84 deletions(-) diff --git a/lib/debugStore/debugStore.d.ts b/lib/debugStore/debugStore.d.ts index 9cc03ce8..bc344c4a 100644 --- a/lib/debugStore/debugStore.d.ts +++ b/lib/debugStore/debugStore.d.ts @@ -24,6 +24,6 @@ export declare class DebugStore> registerTrigger(trigger: Trigger): void; registerChecker(checker: Checker): void; startInitializing(): void; - endInitalizing(): void; + endInitializing(): void; } export {}; diff --git a/lib/debugStore/debugStore.js b/lib/debugStore/debugStore.js index 979fc22e..71193d9f 100644 --- a/lib/debugStore/debugStore.js +++ b/lib/debugStore/debugStore.js @@ -85,7 +85,7 @@ class DebugStore extends oak_memory_tree_store_1.TreeStore { startInitializing() { this.rwLock.acquire('X'); } - endInitalizing() { + endInitializing() { this.rwLock.release(); } } diff --git a/lib/debugStore/index.js b/lib/debugStore/index.js index beb79714..4c11927c 100644 --- a/lib/debugStore/index.js +++ b/lib/debugStore/index.js @@ -22,10 +22,10 @@ async function initDataInStore(store, createContext, initialData) { } await context.commit(); } - store.endInitalizing(); + store.endInitializing(); } function getMaterializedData() { - if ( /* process.env.OAK_PLATFORM === 'weChatMp' */true) { + if (process.env.OAK_PLATFORM === 'weChatMp') { try { const data = wx.getStorageSync('debugStore'); const stat = wx.getStorageSync('debugStoreStat'); @@ -41,10 +41,26 @@ function getMaterializedData() { return; } } + else if (process.env.OAK_PLATFORM === 'web') { + try { + const data = JSON.parse(window.localStorage.getItem('debugStore')); + const stat = JSON.parse(window.localStorage.getItem('debugStoreStat')); + if (data && stat) { + return { + data, + stat, + }; + } + return; + } + catch (e) { + return; + } + } } let lastMaterializedVersion = 0; function materializeData(data, stat) { - if ( /* process.env.OAK_PLATFORM === 'weChatMp' */true) { + if (process.env.OAK_PLATFORM === 'weChatMp') { try { wx.setStorageSync('debugStore', data); wx.setStorageSync('debugStoreStat', stat); @@ -62,6 +78,18 @@ function materializeData(data, stat) { }); } } + else if (process.env.OAK_PLATFORM === 'web') { + try { + window.localStorage.setItem('debugStore', typeof data === 'string' ? data : JSON.stringify(data)); + window.localStorage.setItem('debugStoreStat', JSON.stringify(stat)); + lastMaterializedVersion = stat.commit; + alert('数据已物化'); + } + catch (e) { + console.error(e); + alert('物化数据失败'); + } + } } /** * 在debug环境上创建watcher diff --git a/lib/index.js b/lib/index.js index 976d5130..7151cb6a 100644 --- a/lib/index.js +++ b/lib/index.js @@ -25,9 +25,3 @@ __exportStar(require("./types/Feature"), exports); __exportStar(require("./types/ExceptionRoute"), exports); __exportStar(require("./features/cache"), exports); __exportStar(require("./features/upload"), exports); -/* export { - initI18nWechatMp, - getI18nInstanceWechatMp, - I18nWechatMpRuntimeBase, - I18nWechatMp, -} from './platforms/wechatMp/i18n'; */ diff --git a/lib/initialize.js b/lib/initialize.js index 838164ee..82fb0a67 100644 --- a/lib/initialize.js +++ b/lib/initialize.js @@ -37,16 +37,16 @@ function createAspectProxy(storageSchema, createContext, triggers, checkers, wat return async (params, scene) => { const runningContext = createContext(debugStore, scene); await runningContext.begin(); - let aspectCompeleted = false; + let aspectCompleted = false; try { const result = await aspect(params, runningContext); await runningContext.commit(); - aspectCompeleted = true; + aspectCompleted = true; await features.cache.sync(runningContext.opRecords); return result; } catch (err) { - if (!aspectCompeleted) { + if (!aspectCompleted) { await runningContext.rollback(); } if (err instanceof types_1.OakRowInconsistencyException) { diff --git a/lib/platforms/wechatMp/i18n/index.d.ts b/lib/platforms/wechatMp/i18n/index.d.ts index 21c65666..4faf4a8d 100644 --- a/lib/platforms/wechatMp/i18n/index.d.ts +++ b/lib/platforms/wechatMp/i18n/index.d.ts @@ -25,6 +25,11 @@ export declare class I18nWechatMpRuntimeBase implements CommonI18nInterface { t(key: string, options?: object): string; getFallbackLocale(): string; } +declare global { + const OakI18n: { + i18nInstance: I18nWechatMpRuntimeBase | null; + }; +} export declare function initI18nWechatMp(options: { locales: Locales; defaultLocale?: string; diff --git a/lib/platforms/wechatMp/i18n/index.js b/lib/platforms/wechatMp/i18n/index.js index 55a4c5f2..513bc789 100644 --- a/lib/platforms/wechatMp/i18n/index.js +++ b/lib/platforms/wechatMp/i18n/index.js @@ -12,7 +12,7 @@ class I18nWechatMpRuntimeBase { translations; currentLocale; fallbackLocale; - constructor(translations = {}, currentLocale = "zh_CN" /* Locale.default */, fallbackLocale = "zh_CN" /* Locale.default */) { + constructor(translations = {}, currentLocale = "zh_CN" /* default */, fallbackLocale = "zh_CN" /* default */) { this.translations = translations; this.currentLocale = currentLocale; this.fallbackLocale = fallbackLocale; diff --git a/lib/platforms/wechatMp/index.d.ts b/lib/platforms/wechatMp/index.d.ts index 3a043a80..9c10d630 100644 --- a/lib/platforms/wechatMp/index.d.ts +++ b/lib/platforms/wechatMp/index.d.ts @@ -1,10 +1,4 @@ /// -/// -/// -/// -/// -/// -/// import './polyfill'; import { Aspect, Checker, Context, EntityDict, RowStore, StorageSchema, Trigger, ActionDictOfEntityDict, DeduceSorterItem, DeduceOperation, SelectRowShape, Watcher } from "oak-domain/lib/types"; import { Feature } from '../../types/Feature'; @@ -13,6 +7,8 @@ import { BasicFeatures } from "../../features"; import { ExceptionRouters } from '../../types/ExceptionRoute'; import { NamedFilterItem, NamedSorterItem } from '../../types/NamedCondition'; import { CreateNodeOptions } from '../../features/runningTree'; +import { initI18nWechatMp, I18nWechatMpRuntimeBase, getI18nInstanceWechatMp } from './i18n/index'; +export { initI18nWechatMp, getI18nInstanceWechatMp, I18nWechatMpRuntimeBase, }; declare type RowSelected> = SelectRowShape | undefined; declare type OakComponentOption, AD extends Record>, FD extends Record>, FormedData extends WechatMiniprogram.Component.DataOption, IsList extends boolean> = { entity: T; @@ -159,4 +155,3 @@ export declare function initialize = Partial & Partial> & Partial> & Partial & Partial & ThisType>; export declare type MakeOakPage, AD extends Record>, FD extends Record>> = (options: OakPageOption & ThisType, IS & OakPageInstanceProperties, true>>, componentOptions: OakWechatMpOptions, OakPageData & FormedData, OakPageInstanceProperties, IS, true>) => string; export declare type MakeOakComponent, AD extends Record>, FD extends Record>> = (options: OakComponentOption & ThisType, IS & OakPageInstanceProperties, true>>, componentOptions: OakWechatMpOptions, OakComponentData & FormedData, OakComponentInstanceProperties, IS, false>) => string; -export {}; diff --git a/lib/platforms/wechatMp/index.js b/lib/platforms/wechatMp/index.js index 08296bb0..354c7553 100644 --- a/lib/platforms/wechatMp/index.js +++ b/lib/platforms/wechatMp/index.js @@ -3,13 +3,16 @@ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); -exports.initialize = void 0; +exports.initialize = exports.I18nWechatMpRuntimeBase = exports.getI18nInstanceWechatMp = exports.initI18nWechatMp = void 0; require("./polyfill"); const types_1 = require("oak-domain/lib/types"); const initialize_1 = require("../../initialize"); const assert_1 = __importDefault(require("assert")); const lodash_1 = require("lodash"); const index_1 = require("./i18n/index"); +Object.defineProperty(exports, "initI18nWechatMp", { enumerable: true, get: function () { return index_1.initI18nWechatMp; } }); +Object.defineProperty(exports, "I18nWechatMpRuntimeBase", { enumerable: true, get: function () { return index_1.I18nWechatMpRuntimeBase; } }); +Object.defineProperty(exports, "getI18nInstanceWechatMp", { enumerable: true, get: function () { return index_1.getI18nInstanceWechatMp; } }); ; function callPicker(features, attr, params, entity, parent) { const relation = features.cache.judgeRelation(entity, attr); @@ -479,11 +482,15 @@ function createPageOptions(options, doSubscribe, features, exceptionRouterDict) oakEntity: node.getEntity(), oakFullpath: path2, oakFrom, - newOakActions: oakActions && JSON.parse(oakActions).length > 0 ? JSON.parse(oakActions) : options.actions || [], + newOakActions: oakActions && JSON.parse(oakActions).length > 0 + ? JSON.parse(oakActions) + : options.actions || [], + }, () => { + this.isReady = true; + if (this.isReady) { + this.refresh(); + } }); - if (this.isReady) { - this.refresh(); - } }, ...makeComponentMethods(features, doSubscribe, formData, exceptionRouterDict), }, @@ -524,7 +531,7 @@ function createPageOptions(options, doSubscribe, features, exceptionRouterDict) }; return componentOptions; } -function createComponentOptions(options, features, doSubscribe, exceptionRouterDict) { +function createComponentOptions(options, doSubscribe, features, exceptionRouterDict) { const { formData, entity } = options; const componentOptions = { properties: { @@ -533,21 +540,25 @@ function createComponentOptions(options, features, doSubscribe, exceptionRouterD oakParent: String, }, observers: { - "oakPath": function (path) { + oakPath: function (path) { return this.onPropsChanged({ path, }); }, - "oakParent": function (parent) { + oakParent: function (parent) { return this.onPropsChanged({ parent, }); - } + }, }, methods: { async onPropsChanged(options) { - const path2 = options.hasOwnProperty('path') ? options.path : this.data.oakPath; - const parent2 = options.hasOwnProperty('parent') ? options.parent : this.data.oakParent; + const path2 = options.hasOwnProperty('path') + ? options.path + : this.data.oakPath; + const parent2 = options.hasOwnProperty('parent') + ? options.parent + : this.data.oakParent; if (path2 && parent2) { const oakFullpath2 = `${parent2}.${path2}`; if (oakFullpath2 !== this.data.oakFullpath) { @@ -559,7 +570,7 @@ function createComponentOptions(options, features, doSubscribe, exceptionRouterD } } }, - ...makeComponentMethods(features, doSubscribe, formData, exceptionRouterDict) + ...makeComponentMethods(features, doSubscribe, formData, exceptionRouterDict), }, lifetimes: { async created() { @@ -598,7 +609,7 @@ function createComponentOptions(options, features, doSubscribe, exceptionRouterD }, hide() { this.unsubscribe(); - } + }, }, }; return componentOptions; @@ -724,7 +735,7 @@ function initialize(storageSchema, createFeatures, createContext, exceptionRoute }); }, OakComponent: (options, componentOptions = {}) => { - const oakOptions = createComponentOptions(options, features, subscribe, exceptionRouterDict); + const oakOptions = createComponentOptions(options, subscribe, features, exceptionRouterDict); const { properties, pageLifetimes, lifetimes, methods, data, observers } = oakOptions; const { properties: p2, pageLifetimes: pl2, lifetimes: l2, methods: m2, data: d2, observers: o2, ...restOptions } = componentOptions; const pls = [pageLifetimes, pl2].filter(ele => !!ele); diff --git a/src/debugStore/debugStore.ts b/src/debugStore/debugStore.ts index 975106cb..f7c013f9 100644 --- a/src/debugStore/debugStore.ts +++ b/src/debugStore/debugStore.ts @@ -113,7 +113,7 @@ export class DebugStore> extends this.rwLock.acquire('X'); } - endInitalizing() { + endInitializing() { this.rwLock.release(); } } diff --git a/src/debugStore/index.ts b/src/debugStore/index.ts index 87eabdb5..ea0a5b0c 100644 --- a/src/debugStore/index.ts +++ b/src/debugStore/index.ts @@ -26,11 +26,11 @@ async function initDataInStore>( } await context.commit(); } - store.endInitalizing(); + store.endInitializing(); } function getMaterializedData() { - if (/* process.env.OAK_PLATFORM === 'weChatMp' */true) { + if (process.env.OAK_PLATFORM === 'weChatMp') { try { const data = wx.getStorageSync('debugStore'); const stat = wx.getStorageSync('debugStoreStat'); @@ -46,12 +46,31 @@ function getMaterializedData() { return; } } + else if (process.env.OAK_PLATFORM === 'web') { + try { + const data = JSON.parse( + window.localStorage.getItem('debugStore') as string + ); + const stat = JSON.parse( + window.localStorage.getItem('debugStoreStat') as string + ); + if (data && stat) { + return { + data, + stat, + }; + } + return; + } catch (e) { + return; + } + } } let lastMaterializedVersion = 0; function materializeData(data: any, stat: { create: number, update: number, remove: number, commit: number }) { - if (/* process.env.OAK_PLATFORM === 'weChatMp' */true) { + if (process.env.OAK_PLATFORM === 'weChatMp') { try { wx.setStorageSync('debugStore', data); wx.setStorageSync('debugStoreStat', stat); @@ -69,6 +88,17 @@ function materializeData(data: any, stat: { create: number, update: number, remo }); } } + else if (process.env.OAK_PLATFORM === 'web') { + try { + window.localStorage.setItem('debugStore', typeof data === 'string' ? data : JSON.stringify(data)); + window.localStorage.setItem('debugStoreStat', JSON.stringify(stat)); + lastMaterializedVersion = stat.commit; + alert('数据已物化'); + } catch (e) { + console.error(e); + alert('物化数据失败'); + } + } } /** diff --git a/src/index.ts b/src/index.ts index 9e4b3996..67492913 100644 --- a/src/index.ts +++ b/src/index.ts @@ -11,9 +11,4 @@ export * from './types/ExceptionRoute'; export { BasicFeatures } from './features'; export * from './features/cache'; export * from './features/upload'; -/* export { - initI18nWechatMp, - getI18nInstanceWechatMp, - I18nWechatMpRuntimeBase, - I18nWechatMp, -} from './platforms/wechatMp/i18n'; */ + diff --git a/src/initialize.ts b/src/initialize.ts index 99cb6002..b01a4e83 100644 --- a/src/initialize.ts +++ b/src/initialize.ts @@ -36,18 +36,18 @@ function createAspectProxy, return async (params: Parameters[0], scene: string) => { const runningContext = createContext(debugStore, scene); await runningContext.begin(); - let aspectCompeleted = false; + let aspectCompleted = false; try { const result = await aspect(params, runningContext); await runningContext.commit(); - aspectCompeleted = true; + aspectCompleted = true; await features.cache.sync(runningContext.opRecords); return result; } catch(err) { - if (!aspectCompeleted) { - await runningContext.rollback(); + if (!aspectCompleted) { + await runningContext.rollback(); } if (err instanceof OakRowInconsistencyException) { // 在这里可以同步相应的数据 todo diff --git a/src/platforms/wechatMp/i18n/index.ts b/src/platforms/wechatMp/i18n/index.ts index 6c129a82..5d2f1460 100644 --- a/src/platforms/wechatMp/i18n/index.ts +++ b/src/platforms/wechatMp/i18n/index.ts @@ -91,6 +91,12 @@ export class I18nWechatMpRuntimeBase implements CommonI18nInterface { } } +declare global { + const OakI18n: { + i18nInstance: I18nWechatMpRuntimeBase | null; + }; +} + export function initI18nWechatMp(options: { locales: Locales; defaultLocale?: string; diff --git a/src/platforms/wechatMp/index.ts b/src/platforms/wechatMp/index.ts index 18849b74..8d549abc 100644 --- a/src/platforms/wechatMp/index.ts +++ b/src/platforms/wechatMp/index.ts @@ -9,7 +9,19 @@ import { assign, union } from "lodash"; import { ExceptionHandler, ExceptionRouters } from '../../types/ExceptionRoute'; import { NamedFilterItem, NamedSorterItem } from '../../types/NamedCondition'; import { CreateNodeOptions } from '../../features/runningTree'; -import { getI18nInstanceWechatMp, CURRENT_LOCALE_KEY, CURRENT_LOCALE_DATA } from './i18n/index'; +import { + initI18nWechatMp, + I18nWechatMpRuntimeBase, + getI18nInstanceWechatMp, + CURRENT_LOCALE_KEY, + CURRENT_LOCALE_DATA, +} from './i18n/index'; + +export { + initI18nWechatMp, + getI18nInstanceWechatMp, + I18nWechatMpRuntimeBase, +}; type RowSelected< ED extends EntityDict, @@ -750,15 +762,23 @@ function createPageOptions 0 ? JSON.parse(oakActions) : options.actions || [], - }); - if (this.isReady) { - this.refresh(); - } + this.setData( + { + oakEntity: node.getEntity(), + oakFullpath: path2, + oakFrom, + newOakActions: + oakActions && JSON.parse(oakActions).length > 0 + ? JSON.parse(oakActions) + : options.actions || [], + }, + () => { + this.isReady = true; + if (this.isReady) { + this.refresh(); + } + } + ); }, ...makeComponentMethods(features, doSubscribe, formData as any, exceptionRouterDict), @@ -808,17 +828,20 @@ function createPageOptions, AD extends Record>, FD extends Record>, IsList extends boolean, - FormedData extends WechatMiniprogram.Component.DataOption>( - options: OakComponentOption, - features: BasicFeatures & FD, - doSubscribe: ReturnType['subscribe'], - exceptionRouterDict: Record) { + FormedData extends WechatMiniprogram.Component.DataOption +>( + options: OakComponentOption, + doSubscribe: ReturnType['subscribe'], + features: BasicFeatures & FD, + exceptionRouterDict: Record +) { const { formData, entity } = options; const componentOptions: WechatMiniprogram.Component.Options< @@ -833,21 +856,25 @@ function createComponentOptions, AD ex IS, true > = {}) => { - const oakOptions = createComponentOptions(options, features, subscribe, exceptionRouterDict); + const oakOptions = createComponentOptions(options, subscribe, features, exceptionRouterDict); const { properties, pageLifetimes, lifetimes, methods, data, observers } = oakOptions; const { properties: p2, pageLifetimes: pl2, lifetimes: l2, methods: m2, data: d2, observers: o2, ...restOptions } = componentOptions; diff --git a/src/typings/polyfill.d.ts b/src/typings/polyfill.d.ts index 3767731b..b15d3e50 100644 --- a/src/typings/polyfill.d.ts +++ b/src/typings/polyfill.d.ts @@ -1,11 +1,5 @@ -import { I18nWechatMpRuntimeBase } from '../platforms/wechatMp/i18n'; declare global { const generateNewId: (options?: { timestamp?: boolean }) => Promise; - const OakI18n: { - i18nInstance: I18nWechatMpRuntimeBase | null; - } = { - i18nInstance: null, - }; } export {}; diff --git a/tsconfig.json b/tsconfig.json index 938be8a1..2e7b5d31 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -70,7 +70,9 @@ "node", "miniprogram-api-typings" ], - "include": [ "src/**/*.ts" ], + "include": [ + "src/**/*.ts", + ], "exclude": [ "node_modules", "**/*.spec.ts",