Skip to content

Commit

Permalink
make @wq/model ORM a plugin & add reset()
Browse files Browse the repository at this point in the history
  • Loading branch information
sheppard committed Dec 1, 2022
1 parent 49b1af2 commit b6d2877
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 68 deletions.
43 changes: 2 additions & 41 deletions packages/app/src/app.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import ds from '@wq/store';
import modelModule from '@wq/model';
import orm from '@wq/model';
import outbox from '@wq/outbox';
import router from '@wq/router';
import spinner from './spinner';
Expand All @@ -22,9 +22,8 @@ var app = {
};

const SERVER = '@@SERVER';
const CORE_PLUGINS = ['renderer'];
const CORE_PLUGINS = ['orm', 'renderer'];

app.models = {};
app.plugins = {};

var _register = {};
Expand Down Expand Up @@ -221,7 +220,6 @@ app.init = function (config) {
(conf.server_modes || []).forEach(function (mode) {
_register.detail(page, mode, _serverContext);
});
app.models[page] = modelModule(conf);
} else if (conf) {
_registerOther(page);
}
Expand Down Expand Up @@ -295,26 +293,6 @@ app.use = function (plugin) {
}
};

app.prefetchAll = async function (message) {
if (message) {
app.spin.start(message);
}
const result = await Promise.all(
Object.keys(app.models).map(function (name) {
return app.models[name].prefetch();
})
);
if (message) {
app.spin.stop(message);
}
return result;
};

app.jqmInit = function () {
console.warn(new Error('jqmInit() renamed to start()'));
app.start();
};

app.start = function () {
router.start();
app.callPlugins('start');
Expand All @@ -330,19 +308,6 @@ async function _getSyncInfo() {
};
}

app.go = function () {
throw new Error('app.go() has been removed. Use app.nav() instead');
};

// Sync outbox and handle result
app.sync = function (retryAll) {
if (retryAll) {
console.warn('app.sync(true) renamed to app.retryAll()');
app.retryAll();
} else {
throw new Error('app.sync() no longer used.');
}
};
app.retryAll = function () {
app.outbox.unsynced().then(function (unsynced) {
if (!unsynced) {
Expand Down Expand Up @@ -557,10 +522,6 @@ app.nav = function (url) {
router.push(url);
};

app.replaceState = function (url) {
throw new Error('app.replaceState() no longer supported.');
};

app.refresh = function () {
router.refresh();
};
Expand Down
3 changes: 0 additions & 3 deletions packages/model/index.js

This file was deleted.

2 changes: 1 addition & 1 deletion packages/model/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.3.0",
"description": "A simple model API for working with stored lists",
"type": "module",
"main": "index.js",
"main": "src/index.js",
"scripts": {
"test": "cd ../../ && npm run jest packages/model",
"prettier": "cd ../../ && npm run prettier -- --write packages/model/",
Expand Down
53 changes: 53 additions & 0 deletions packages/model/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { Model, model as createModel } from './model.js';

const orm = {
// Plugin attributes
name: 'orm',
type: 'orm',
init() {
for (const conf of Object.values(this.app.config.pages)) {
if (!conf.list) {
continue;
}
const model = createModel(conf);
this.models[conf.name] = model;
if (!this._orm) {
this._orm = model.orm;
}
}
this.app.models = this.models;
this.app.prefetchAll = message => this.prefetchAll(message);
this.app.resetAll = () => this.reset();
},
actions: {
reset() {
return { type: 'ORM_RESET' };
}
},
reducer(state, action) {
if (!this._orm) {
return state;
}
return this._orm.reducer(state, action);
},
persist: true,

// Custom attributes
models: {},
async prefetchAll(message) {
if (message) {
this.app.spin.start(message);
}
const result = await Promise.all(
Object.values(this.models).map(model => model.prefetch())
);
if (message) {
this.app.spin.stop(message);
}
return result;
}
};

export default orm;

export { Model, createModel, createModel as model };
19 changes: 7 additions & 12 deletions packages/model/src/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,12 @@ class ORMWithReducer extends ORM {
return `${this.store.name.toUpperCase()}ORM`;
}
}
reducer(state, action) {
const session = this.session(state || this.getEmptyState()),
reducer(lastState, action) {
const state =
action.type === `${this.prefix}_RESET` || !lastState
? this.getEmptyState()
: lastState,
session = this.session(state),
match = action.type.match(/^([^_]+)_(.+)_([^_]+)$/);

if (!match || match[1] !== this.prefix) {
Expand Down Expand Up @@ -255,11 +259,6 @@ ModelMeta.modelName = '_modelmeta';
function orm(store) {
if (!_orms[store.name]) {
const orm = (_orms[store.name] = new ORMWithReducer(store));
store.addReducer(
'orm',
(state, action) => orm.reducer(state, action),
true
);
orm.register(ModelMeta);
}
return _orms[store.name];
Expand Down Expand Up @@ -847,8 +846,4 @@ function isPotentialNumber(value) {
return typeof value !== 'number' && !Number.isNaN(+value);
}

model.Model = Model;

export default model;

export { Model };
export { model, Model };
2 changes: 1 addition & 1 deletion packages/outbox/src/outbox.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import ds from '@wq/store';
import model from '@wq/model';
import { model } from '@wq/model';
import { convert } from '../vendor/json-forms';
import { createOffline, offlineConfig, RESET_STATE, busy } from './offline';

Expand Down
17 changes: 9 additions & 8 deletions packages/react/src/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,14 +214,15 @@ export function useBreadcrumbs() {
}

export function useSitemap() {
const config = useConfig(),
pages = Object.values(config.pages).filter(
page => page.show_in_index !== false
),
options = pages.filter(page => !page.list),
models = pages.filter(page => page.list);

return { options, models };
const config = useConfig();
return useMemo(() => {
const pages = Object.values(config.pages).filter(
page => page.show_in_index !== false
),
options = pages.filter(page => !page.list),
models = pages.filter(page => page.list);
return { options, models };
}, [config]);
}

export function useSpinner() {
Expand Down
2 changes: 2 additions & 0 deletions packages/react/src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react';
import orm from '@wq/model';
import { Provider as StoreProvider } from 'react-redux';
import { AppContext } from './hooks';

Expand All @@ -15,6 +16,7 @@ import validate from './validate';
export default {
name: 'react',
type: 'renderer',
dependencies: [orm],

config: {
messages: { ...messages }
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/inputs/ForeignKey.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function useSelectInput(component) {
let Component;
if (typeof component === 'string') {
Component = inputs[component];
if (!component) {
if (!Component) {
Component = function UnknownInput(props) {
return (
<Select
Expand Down
2 changes: 1 addition & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const deps = {
'@wq/app': './packages/app/src/app.js',
'@wq/store': './packages/store/src/store.js',
'@wq/router': './packages/router/src/router.js',
'@wq/model': './packages/model/src/model.js',
'@wq/model': './packages/model/src/index.js',
'@wq/outbox': './packages/outbox/src/outbox.js',
'@wq/react': './packages/react/src/index.js',
'@wq/material': './packages/material/src/index.js',
Expand Down

0 comments on commit b6d2877

Please sign in to comment.