singleton<T extends Object?> method

T? singleton<T extends Object?>(
  1. InstanceBuilder<T> builder, {
  2. String? id,
  3. Object? ref,
})

It's a ways to manage a instance, which registers a builder function and creates the instance only once.

This mode preserves the instance and its states, even if the dependency tree stops using it.

Use Reactter.destroy if you want to force destroy the instance and its register.

It consumes less CPU than builder and factory, but uses more RAM than the other modes.

Under the following conditions:

  • if not found and hasn't registered it, registers, creates and returns it.
  • if not found and has registered it, creates and returns it.
  • if found it, returns it.
  • else return null.

It's a ways to manage a instance, which registers a builder function and creates the instance only once.

This mode preserves the instance and its states, even if the dependency tree stops using it.

Use Reactter.destroy if you want to force destroy the instance and its register.

It consumes less CPU than builder and factory, but uses more RAM than the other modes.

This method is equivalent to calling:

Reactter.create<T>(
  builder,
  id: id,
  ref: ref,
  mode: InstanceManageMode.singleton,
);

Implementation

T? singleton<T extends Object?>(
  InstanceBuilder<T> builder, {
  String? id,
  Object? ref,
}) {
  return create<T>(
    builder,
    id: id,
    ref: ref,
    mode: InstanceManageMode.singleton,
  );
}