Pocket Redux

  • 7 Replies
  • 3918 Views
*

Zero

  • Eve
  • ***********
  • 1287
Pocket Redux
« on: March 30, 2022, 09:02:35 pm »
Now that I have a central blackboard, I don't want random parts of my program to mess with my state. So, inspired from Redux, here is an intent manager.

Code
// Intent manager

class IntentManager {

    intentions = [];
    target = null;
    actions = {};
    intentID = 0n;

    constructor(target) {
        this.target = target;
    }

    intend(action, parameters) {
        this.intentions.push({ action, parameters, id: 'i' + this.intentID });
        return 'i' + (this.intentID++);
    }

    action(id, handler) {
        this.actions[id] = handler;
    }

    execute(intent) {
        return this.actions[intent.action](intent.parameters, this.target, this);
    }

    run() {
        let intentList = Array.from(this.intentions);
        this.intentions = [];
        let result = {};
        for (let intent of intentList)
            result[intent.id] = { intent, result: this.execute(intent) };
        return result;
    }
}

The point (in my use case) is to separate the choice of an action from the execution of an action. The only (but big) difference between my IntentManager and ReduxJS, is that Redux wants you to use an immutable central store. In my use case, this is not an option because I consider that cloning the entire mind of an Ai every time you're going to increment some ridiculous counter somewhere, is not realistic. But, even with a mutable store, you still get all the expected benefits of the pattern, as long as you stick to the rules.

The manager doesn't enforce anything, but you're supposed to respect 3 simple rules to use it correctly.

1) All of your stuff should be in one place, which is the target of the intent manager. In my case, this is an SMap (which I talked about in the other thread) but of course it could be anything else.

2) Access to the store should be done only through actions. It's especially important when things are triggered from outside events, which could occur right in the middle of a dbase mutation. Simple enough.

3) Action handlers should always give the same result when given the same arguments. I'm not saying they should be pure though, because I don't care about side-effects.

And... that's about it! The "action" becomes the processing unit inside of which you're safe: inside of an action, you cannot encounter inconsistent data in the store.

Edit: added return values.
« Last Edit: March 31, 2022, 04:43:10 pm by Zero »

*

MagnusWootton

  • Replicant
  • ********
  • 646
Re: Pocket Redux
« Reply #1 on: March 30, 2022, 11:30:46 pm »
I dont see the importance of this,  how can the state be changed unless u did it on purpose?    ?

*

Zero

  • Eve
  • ***********
  • 1287
Re: Pocket Redux
« Reply #2 on: March 31, 2022, 01:35:46 am »
This is mainly about race condition...

https://en.m.wikipedia.org/wiki/Race_condition#In_software

https://medium.com/@slavik57/async-race-conditions-in-javascript-526f6ed80665

These two links should explain it better than I can   ::)

*

MagnusWootton

  • Replicant
  • ********
  • 646
Re: Pocket Redux
« Reply #3 on: March 31, 2022, 01:58:20 am »
Ah, so uve got a parallel implementation.   I see now.

*

Zero

  • Eve
  • ***********
  • 1287
Re: Pocket Redux
« Reply #4 on: March 31, 2022, 09:17:58 pm »
I've been thinking about your input here. To be perfectly honest, nothing can be really parallel in Javascript, since it is single threaded. But the IntentManager makes the call sequences explicit, which is a good thing. What matters is the order in which things are done.

*

ivan.moony

  • Trusty Member
  • ************
  • Bishop
  • *
  • 1729
    • mind-child
Re: Pocket Redux
« Reply #5 on: April 01, 2022, 06:50:13 am »
Javascript supports multitasking for a while now. See webworkers. Though, we can't manipulate DOM from webworkers.

*

Zero

  • Eve
  • ***********
  • 1287
Re: Pocket Redux
« Reply #6 on: April 01, 2022, 09:11:08 am »
Yes I know that, I even made a game using web workers. But they are isolated, and when you want to retrieve data from them, you do it from the main single thread. In Javascript, you cannot have two pieces of code trying to access the same resource at the same time.

*

MagnusWootton

  • Replicant
  • ********
  • 646
Re: Pocket Redux
« Reply #7 on: April 01, 2022, 07:19:50 pm »
you cannot have two pieces of code trying to access the same resource at the same time.

Its cause they are happening in parallel.

 


Requirements for functional equivalence to conscious processing?
by DaltonG (General AI Discussion)
November 19, 2024, 11:56:05 am
Will LLMs ever learn what is ... is?
by HS (Future of AI)
November 10, 2024, 06:28:10 pm
Who's the AI?
by frankinstien (Future of AI)
November 04, 2024, 05:45:05 am
Project Acuitas
by WriterOfMinds (General Project Discussion)
October 27, 2024, 09:17:10 pm
Ai improving AI
by infurl (AI Programming)
October 19, 2024, 03:43:29 am
Atronach's Eye
by WriterOfMinds (Home Made Robots)
October 13, 2024, 09:52:42 pm
Running local AI models
by spydaz (AI Programming)
October 07, 2024, 09:00:53 am
Hi IM BAA---AAACK!!
by MagnusWootton (Home Made Robots)
September 16, 2024, 09:49:10 pm
LLaMA2 Meta's chatbot released
by spydaz (AI News )
August 24, 2024, 02:58:36 pm
ollama and llama3
by spydaz (AI News )
August 24, 2024, 02:55:13 pm
AI controlled F-16, for real!
by frankinstien (AI News )
June 15, 2024, 05:40:28 am
Open AI GPT-4o - audio, vision, text combined reasoning
by MikeB (AI News )
May 14, 2024, 05:46:48 am
OpenAI Speech-to-Speech Reasoning Demo
by MikeB (AI News )
March 31, 2024, 01:00:53 pm
Say good-bye to GPUs...
by MikeB (AI News )
March 23, 2024, 09:23:52 am
Google Bard report
by ivan.moony (AI News )
February 14, 2024, 04:42:23 pm
Elon Musk's xAI Grok Chatbot
by MikeB (AI News )
December 11, 2023, 06:26:33 am

Users Online

213 Guests, 0 Users

Most Online Today: 532. Most Online Ever: 2369 (November 21, 2020, 04:08:13 pm)

Articles