github.com/voedger/voedger@v0.0.0-20240520144910-273e84102129/pkg/state/impl_async_actualizer_state.go (about)

     1  /*
     2   * Copyright (c) 2022-present unTill Pro, Ltd.
     3   */
     4  
     5  package state
     6  
     7  import (
     8  	"context"
     9  
    10  	"github.com/voedger/voedger/pkg/appdef"
    11  	"github.com/voedger/voedger/pkg/isecrets"
    12  	"github.com/voedger/voedger/pkg/istructs"
    13  	"github.com/voedger/voedger/pkg/itokens"
    14  	"github.com/voedger/voedger/pkg/state/smtptest"
    15  	"github.com/voedger/voedger/pkg/utils/federation"
    16  )
    17  
    18  type ActualizerStateOptFunc func(opts *actualizerStateOpts)
    19  
    20  func WithEmailMessagesChan(messages chan smtptest.Message) ActualizerStateOptFunc {
    21  	return func(opts *actualizerStateOpts) {
    22  		opts.messages = messages
    23  	}
    24  }
    25  
    26  func WithCustomHttpClient(client IHttpClient) ActualizerStateOptFunc {
    27  	return func(opts *actualizerStateOpts) {
    28  		opts.customHttpClient = client
    29  	}
    30  }
    31  
    32  func WithFedearationCommandHandler(handler FederationCommandHandler) ActualizerStateOptFunc {
    33  	return func(opts *actualizerStateOpts) {
    34  		opts.federationCommandHandler = handler
    35  	}
    36  }
    37  
    38  type actualizerStateOpts struct {
    39  	messages                 chan smtptest.Message
    40  	federationCommandHandler FederationCommandHandler
    41  	customHttpClient         IHttpClient
    42  }
    43  
    44  type asyncActualizerState struct {
    45  	*bundledHostState
    46  	eventFunc PLogEventFunc
    47  }
    48  
    49  func (s *asyncActualizerState) PLogEvent() istructs.IPLogEvent {
    50  	return s.eventFunc()
    51  }
    52  
    53  func implProvideAsyncActualizerState(ctx context.Context, appStructsFunc AppStructsFunc, partitionIDFunc PartitionIDFunc, wsidFunc WSIDFunc, n10nFunc N10nFunc,
    54  	secretReader isecrets.ISecretReader, eventFunc PLogEventFunc, tokensFunc itokens.ITokens, federationFunc federation.IFederation,
    55  	intentsLimit, bundlesLimit int, optFuncs ...ActualizerStateOptFunc) IBundledHostState {
    56  
    57  	opts := &actualizerStateOpts{}
    58  	for _, optFunc := range optFuncs {
    59  		optFunc(opts)
    60  	}
    61  
    62  	state := &asyncActualizerState{
    63  		bundledHostState: &bundledHostState{
    64  			hostState:    newHostState("AsyncActualizer", intentsLimit, appStructsFunc),
    65  			bundlesLimit: bundlesLimit,
    66  			bundles:      make(map[appdef.QName]bundle),
    67  		},
    68  		eventFunc: eventFunc,
    69  	}
    70  
    71  	state.addStorage(View, newViewRecordsStorage(ctx, appStructsFunc, wsidFunc, n10nFunc), S_GET|S_GET_BATCH|S_READ|S_INSERT|S_UPDATE)
    72  	state.addStorage(Record, newRecordsStorage(appStructsFunc, wsidFunc, nil), S_GET|S_GET_BATCH)
    73  
    74  	state.addStorage(WLog, &wLogStorage{
    75  		ctx:        ctx,
    76  		eventsFunc: func() istructs.IEvents { return appStructsFunc().Events() },
    77  		wsidFunc:   wsidFunc,
    78  	}, S_GET|S_READ)
    79  
    80  	state.addStorage(SendMail, &sendMailStorage{
    81  		messages: opts.messages,
    82  	}, S_INSERT)
    83  
    84  	state.addStorage(Http, &httpStorage{
    85  		customClient: opts.customHttpClient,
    86  	}, S_READ)
    87  
    88  	state.addStorage(FederationCommand, &federationCommandStorage{
    89  		appStructs: appStructsFunc,
    90  		wsid:       wsidFunc,
    91  		emulation:  opts.federationCommandHandler,
    92  		federation: federationFunc,
    93  		tokens:     tokensFunc,
    94  	}, S_GET)
    95  
    96  	state.addStorage(AppSecret, &appSecretsStorage{secretReader: secretReader}, S_GET)
    97  
    98  	state.addStorage(Event, &eventStorage{eventFunc: eventFunc}, S_GET)
    99  
   100  	return state
   101  }