some refactoring...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2022-02-24 00:02:20 +03:00
parent e7b0c67ef1
commit af109c13f8

View File

@ -1148,14 +1148,40 @@ var CacheActions = actions.Actions({
cache: doc('Get or set cache value', cache: doc('Get or set cache value',
doc`Get or set cache value doc`Get or set cache value
Get cached value in global group...
.cache(title)
.cache('global', title)
-> value
-> undefined
Get cached value in a specific group...
.cache(group, title)
-> value
-> undefined
Get/set cached value in the global group...
.cache(title, handler) .cache(title, handler)
.cache('global', title, handler)
-> value -> value
Get/set cached value in a specific group...
.cache(group, title, handler) .cache(group, title, handler)
-> value -> value
handler(value)
-> value
Handler calls will overwrite the cached value with the handler
returned value on every call, this is different to pure getters
that will only fetch a value if it exists.
Currently the used groups are: Currently the used groups are:
Global group -- default group
global
Session groups -- cleared on .clear() (feature: 'cache') Session groups -- cleared on .clear() (feature: 'cache')
session-* session-*
view-* view-*
@ -1192,19 +1218,29 @@ var CacheActions = actions.Actions({
not logical... not logical...
`, `,
function(title, handler){ function(title, handler){
var args = [...arguments]
var handler = args.pop()
var group = 'global' var group = 'global'
args.length == 2
&& ([group, title] = args)
// caching disabled... // caching disabled...
if(!(this.config || {}).cache){ if(!(this.config || {}).cache){
return handler.call(this) } return typeof(handler) != 'function' ?
arguments.length > 2 undefined
&& ([group, title, handler] = arguments) : handler.call(this) }
// get...
if(typeof(handler) != 'function'){
return ((this.__cache || {})[group] || {})[handler]
// handle...
} else {
var cache = this.__cache = this.__cache || {} var cache = this.__cache = this.__cache || {}
cache = cache[group] = cache[group] || {} cache = cache[group] = cache[group] || {}
return (cache[title] = return (cache[title] =
title in cache ? title in cache ?
// pass the cached data for cloning/update to the handler... // pass the cached data for cloning/update to the handler...
handler.call(this, cache[title]) handler.call(this, cache[title])
: handler.call(this)) }), : handler.call(this)) } }),
clearCache: ['System/Clear cache', clearCache: ['System/Clear cache',
doc` doc`
@ -1965,20 +2001,25 @@ var JournalActions = actions.Actions({
res.unshift(e) } res.unshift(e) }
return res }, return res },
// XXX make this a cached prop... (???) get journalable(){
journalable: null, return this.cache('journalable-actions', function(data){
return data ?
data.slice()
: this.updateJournalableActions() }) },
// XXX docs...
// XXX <action>.getUndoState(..) should be called for every action // XXX <action>.getUndoState(..) should be called for every action
// in chain... // in chain...
// XXX should aliases support explicit undo??? (test) // XXX should aliases support explicit undo??? (test)
updateJournalableActions: ['System/Update list of journalable actions', updateJournalableActions: ['- System/',
doc` doc`Update journalable actions
This will setup the action journal handler as a .pre handler This will setup the action journal handler as a .pre handler
(tagged: 'journal-handler'), calling this again will reset the existing (tagged: 'journal-handler').
handlers and add new ones.
NOTE: calling this again will reset the existing handlers and add
new ones.
NOTE: the list of journalable actions is cached and accessible via
.journalable prop and the cache API, e.g. via .cache('journalable-actions').
NOTE: action aliases can not handle undo. NOTE: action aliases can not handle undo.
.journal / .rjournal format: .journal / .rjournal format:
@ -2012,7 +2053,6 @@ var JournalActions = actions.Actions({
`, `,
function(){ function(){
var that = this var that = this
var handler = function(action){ var handler = function(action){
return function(){ return function(){
var data = { var data = {
@ -2044,7 +2084,10 @@ var JournalActions = actions.Actions({
&& update.call(that, data) && update.call(that, data)
this.journalPush(data) } } } this.journalPush(data) } } }
this.journalable = this.actions return this
// NOTE: we will overwrite the cache on every call...
.cache('journalable-actions', function(){
return this.actions
.filter(function(action){ .filter(function(action){
// remove all existing journal handlers before we setup again... // remove all existing journal handlers before we setup again...
that.off(action+'.pre', 'journal-handler') that.off(action+'.pre', 'journal-handler')
@ -2055,7 +2098,7 @@ var JournalActions = actions.Actions({
// set the handler // set the handler
.map(function(action){ .map(function(action){
that.on(action+'.pre', 'journal-handler', handler(action)) that.on(action+'.pre', 'journal-handler', handler(action))
return action }) }], return action }) }) }],
// XXX unify names (globally) -> .journal<Action>(..) or .<action>Journal(..) // XXX unify names (globally) -> .journal<Action>(..) or .<action>Journal(..)
journalPush: ['- System/Journal/Add an item to journal', journalPush: ['- System/Journal/Add an item to journal',
@ -2098,8 +2141,9 @@ var JournalActions = actions.Actions({
[e.action].apply(that, e.args) }) }], [e.action].apply(that, e.args) }) }],
// XXX might be a good idea to add support for: // XXX might be a good idea to add support for:
// - time-periods // - time-periods - DONE
// - specific times // - specific times - DONE
// ...might be a good idea to support date strings directly...
// XXX needs very careful revision... // XXX needs very careful revision...
// - should this be thread safe??? (likely not) // - should this be thread safe??? (likely not)
// - revise actions... // - revise actions...
@ -2115,11 +2159,9 @@ var JournalActions = actions.Actions({
doc`Undo last action(s) from .journal that can be undone doc`Undo last action(s) from .journal that can be undone
.undo() .undo()
.undo(<count>) .undo(<count>)
.undo('<time-period>')
.undo('unsaved') .undo('unsaved')
.undo('all') .undo('all')
@ -2136,6 +2178,15 @@ var JournalActions = actions.Actions({
count = count == 'all' ? count = count == 'all' ?
Infinity Infinity
: count : count
var to =
// time period...
(typeof(count) == 'string'
&& Date.isPeriod(count)) ?
Date.now() - Date.str2ms(count)
// Date...
: count instanceof Date ?
count.valueOf()
: false
// NOTE: these are isolated from any other contexts and will // NOTE: these are isolated from any other contexts and will
// be saved as own attributes... // be saved as own attributes...
var journal = (this.journal || []).slice() || [] var journal = (this.journal || []).slice() || []
@ -2149,6 +2200,9 @@ var JournalActions = actions.Actions({
&& (a == 'SAVED' && (a == 'SAVED'
|| a.type == 'save')){ || a.type == 'save')){
break } break }
// stop at date...
if(to && a.date*1 < to){
break }
// stop at load... // stop at load...
// XXX not sure if this is correct.... // XXX not sure if this is correct....
if(a.action == 'load'){ if(a.action == 'load'){
@ -2189,7 +2243,7 @@ var JournalActions = actions.Actions({
// stop when done... // stop when done...
if(undo if(undo
&& count != 'unsaved' && typeof(count) == 'number'
&& --count <= 0){ && --count <= 0){
break } } break } }
@ -2199,10 +2253,6 @@ var JournalActions = actions.Actions({
// so we will need to restore things... // so we will need to restore things...
this.journal = journal this.journal = journal
this.rjournal = rjournal }], this.rjournal = rjournal }],
// XXX add arg:
// - count - DONE
// - 'all' - DONE
// - 'unsaved'
redo: ['Edit/Redo', redo: ['Edit/Redo',
doc`Redo an action from .rjournal doc`Redo an action from .rjournal
@ -2278,14 +2328,13 @@ module.Journal = ImageGridFeatures.Feature({
handlers: [ handlers: [
// log state, action and its args... // log state, action and its args...
['start', ['start',
function(){ this.updateJournalableActions() }], 'updateJournalableActions'],
// clear journal when clearing... // clear journal when clearing...
// XXX we should be loading new journal instead... // XXX we should be loading new journal instead...
// XXX is this a good idea??? // XXX is this a good idea???
['load clear', ['load clear',
function(){ 'clearJournal'],
this.clearJournal() }],
// log saved event to journal... // log saved event to journal...
['saved', ['saved',